package com.longrise.android.byjk.widget.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.view.View;

import com.longrise.android.byjk.R;
import com.longrise.common.utils.AppUtil;

/**
 * 绘制饼图进度
 */
public class RoundView extends View {
	
	private Paint mOutPaint;
	private int mRoundStorkeColor;

	private int mRoundProgressFillColor;

	private int mTextColor;

	private float mTextSize;

	private float mRoundWidth;

	private int mProgressMax;

	private int mProgress;
	private boolean mTextIsDisplayable;

	private int mStyle;   //  默认空心

	public static final int STROKE = 0;   
	public static final int FILL = 1;
	
	private final int mRing = 360; 

	public RoundView(Context context) {
		this(context, null);
	}

	public RoundView(Context context, AttributeSet attrs) {
		this(context, attrs, 0);
	}

	public RoundView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);

		mOutPaint = new Paint();

		TypedArray mTypedArray = context.obtainStyledAttributes(attrs,
				R.styleable.RoundView);

		mRoundStorkeColor = mTypedArray.getColor(
				R.styleable.RoundView_roundColor, Color.RED);
		mRoundProgressFillColor = mTypedArray.getColor(
				R.styleable.RoundView_roundProgressColor,
				Color.parseColor("#009DF4"));
		mTextColor = mTypedArray.getColor(
				R.styleable.RoundView_textColor,
				Color.parseColor("#009DF4"));
		mTextSize = mTypedArray.getDimension(
				R.styleable.RoundView_textSize, 15);
		mRoundWidth = mTypedArray.getDimension(
				R.styleable.RoundView_roundWidth, 5);
		mProgressMax = mTypedArray.getInteger(R.styleable.RoundView_max, 100);
		mTextIsDisplayable = mTypedArray.getBoolean(
				R.styleable.RoundView_textIsDisplayable, true);
		mStyle = mTypedArray.getInt(R.styleable.RoundView_style, 0);

		mTypedArray.recycle();
	}

	/**
	 * 绘制第一个圆圈
	 * */
	private void drawOutSide(Canvas canvas) throws Exception {
		int x = getWidth() / 2;    //得到半径
		int y = x; // 在同一个点
		int radius = (int) (x - mRoundWidth); // 去除  /2
		mOutPaint.setColor(mRoundStorkeColor); // 画笔颜色
		mOutPaint.setStyle(Paint.Style.FILL); // 空心  STROKE
		mOutPaint.setStrokeWidth(mRoundWidth); // 圆环宽度
		mOutPaint.setAntiAlias(true); // 去除纹路
		// 参数 圆形x坐标 y坐标 半径 画笔
		canvas.drawCircle(x, y, radius + AppUtil.dip2px(3), mOutPaint); // 绘制圆形
//		drawText(canvas, x, y);
		drawInSide(canvas, radius, x, y);
	}

	/**
	 * 绘制文本
	 * */
	private void drawText(Canvas canvas, int x, int y) throws Exception {
		mOutPaint.setStrokeWidth(0);
		mOutPaint.setColor(mTextColor);
		mOutPaint.setTextSize(mTextSize);
		mOutPaint.setTypeface(Typeface.DEFAULT_BOLD);
		// 该值自动计算
		int percent = (int) (((float) mProgress / (float) mProgressMax) * 100);
		float textWidth = mOutPaint.measureText(percent + "%");
		if (mTextIsDisplayable && percent != 0 && mStyle == STROKE) {
			canvas.drawText(percent + "%", x - textWidth / 2, y + mTextSize / 2,
					mOutPaint);
		}
	}

	/**
	 * 绘制环形进度
	 * */
	private void drawInSide(Canvas canvas, int radius, int x, int y) {
		mOutPaint.setStrokeWidth(mRoundWidth); 
		mOutPaint.setColor(mRoundProgressFillColor);
		int inRadius = radius - 5;
		RectF oval = new RectF(x - inRadius, y - inRadius, x + inRadius, y + inRadius); 

		switch (mStyle) {
		case STROKE: {
			mOutPaint.setStyle(Paint.Style.STROKE);
			//
			canvas.drawArc(oval, 0, mRing * mProgress / mProgressMax, false, mOutPaint); 
			break;
		}
		case FILL: {
			mOutPaint.setStyle(Paint.Style.FILL_AND_STROKE);
			if (mProgress != 0)
				//参数
				canvas.drawArc(oval, 0, mRing * mProgress / mProgressMax, true, mOutPaint);
			break;
		}
	 }
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		try {
			drawOutSide(canvas);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	public synchronized int getMax() {
		return mProgressMax;
	}

	public synchronized void setMax(int max) {
		if (max < 0) {
			throw new IllegalArgumentException("max not less than 0");
		}
		this.mProgressMax = max;
	}

	public synchronized int getProgress() {
		return mProgress;
	}

	public synchronized void setProgress(int progress) {
		if (progress < 0) {
			throw new IllegalArgumentException("progress not less than 0");
		}
		if (progress > mProgressMax) {
			progress = mProgressMax;
		}
		if (progress <= mProgressMax) {
			this.mProgress = progress;
			postInvalidate();
		}

	}

	public int getCricleColor() {
		return mRoundStorkeColor;
	}

	public void setCricleColor(int cricleColor) {
		this.mRoundStorkeColor = cricleColor;
	}

	public int getCricleProgressColor() {
		return mRoundProgressFillColor;
	}

	public void setCricleProgressColor(int cricleProgressColor) {
		this.mRoundProgressFillColor = cricleProgressColor;
	}

	public int getTextColor() {
		return mTextColor;
	}

	public void setTextColor(int textColor) {
		this.mTextColor = textColor;
	}

	public float getTextSize() {
		return mTextSize;
	}

	public void setTextSize(float textSize) {
		this.mTextSize = textSize;
	}

	public float getRoundWidth() {
		return mRoundWidth;
	}

	public void setRoundWidth(float roundWidth) {
		this.mRoundWidth = roundWidth;
	}

}
